/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vng.zing.survey.data;

import static com.vng.zing.survey.data.Base.deserialize;
import static com.vng.zing.survey.data.Base.serialize;
import com.vng.zing.survey.thrift.model.Action;
import com.vng.zing.survey.thrift.model.IDSurveyItem;
import com.vng.zing.survey.thrift.model.Error;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import kyotocabinet.DB;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class IDSurveyHostData  extends Base{
    private static final Logger logger_ = Logger.getLogger(IDSurveyData.class);  
    private static final Lock createLock_ = new ReentrantLock();
    private static IDSurveyHostData _instance = null;
    private final DB db;

    public IDSurveyHostData() {
        db = new DB();
        if (!db.open("database/idsurveyhost.kch"+cofigid , DB.OWRITER | DB.OCREATE|DB.OAUTOTRAN)) {
            System.err.println("open error: " + db.error());
        }

    }

    public static IDSurveyHostData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new IDSurveyHostData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }
    public IDSurveyItem getIDSurveyItem(int idUser) {
        try {
            byte[] key = serialize(idUser);
            byte[] value = db.get(key);
            if (value == null) {
                return null;
            }
            IDSurveyItem dSurveyItem = new IDSurveyItem();
            dSurveyItem = (IDSurveyItem) deserialize(value);
            return dSurveyItem;
        } catch (IOException  ex) {
            logger_.error("IDSurveyData.getIDSurveyItem:"+ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IDSurveyData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public Error setIDSurveyItem(int idUser, IDSurveyItem iDSurveyItem) {

        try {
            byte[] key = serialize(idUser);
            byte[] value = serialize(iDSurveyItem);
            if (!db.set(key, value)) {
                return Error.ERR_SYSTEM;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
              logger_.error("IDSurveyData.setIDSurveyItem:"+ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }
      public Error updateIDSurveyItem(int idUser, int idSurvey, Action action)  {
                db.begin_transaction(false);
            try {
                IDSurveyItem dSurveyItem = new IDSurveyItem(new ArrayList<Integer>(200), 0);
                byte[] key = serialize(idUser);
                byte[] value = db.get(key);
                if (value!= null) {
                    try {
                        dSurveyItem = (IDSurveyItem) deserialize(value);
                    } catch (ClassNotFoundException ex) {
                        java.util.logging.Logger.getLogger(IDSurveyData.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if(action == Action.ADD){
                    dSurveyItem.getIdSurveys().add(0, idSurvey);
                    dSurveyItem.setCount(dSurveyItem.getCount()+1);
                }
                else if(action==Action.REMOVE){   
                    if((dSurveyItem.getIdSurveys().remove((Integer)idSurvey))== false){
                        db.end_transaction(true);
                        return  Error.IDSURVEY_NO_EXITS;
                    }
                    if( dSurveyItem.getCount()>0){
                     dSurveyItem.setCount(dSurveyItem.getCount()-1);
                    }
                }
                value = serialize(dSurveyItem);
                if (!db.set(key, value)) {
                      db.end_transaction(true);
                    return Error.ERR_SYSTEM;
                }
                  db.end_transaction(true);
                return Error.SUCCESS;
                
                
            } catch (IOException  ex) {
                logger_.error("IDSurveyData.getIDSurveyItem:"+ex.getMessage(), ex);
            }
            db.end_transaction(true);
         return Error.ERR_SYSTEM;
      }
    
    
}
